package com.snailhd.crawler;

import com.alibaba.fastjson.JSONObject;
import com.snailhd.common.util.CSVParser;
import com.snailhd.common.util.HttpClientUtil;
import com.snailhd.entity.StockDay;
import com.snailhd.mapper.StockDayMapper;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;


/**
 * 下载股票历史数据并解析Csv文件到数据库
 */

@Slf4j
@Service
public class DownloadHistoryCsvAndParseToDBExector {

    @Resource
    StockDayMapper stockDayMapper;

    private final static String DOWNLOAD_HISTORY_CSV = "http://quotes.money.163.com/service/chddata.html";

    public void exec(String code,boolean cover,String start,String end) throws Exception{

        DownloadParam downloadParam = new DownloadParam(code, start, end);

        InputStream is = HttpClientUtil.download("下载股票历史文件",DOWNLOAD_HISTORY_CSV, BeanUtils.describe(downloadParam),null);
        if(is==null) { return; }
        List<StockDay> list = CSVParser.parse(is,"GBK",new CSVParser.Parser<StockDay>(){
            String[] fields= {"date","code","name","trade","high","low","open","settlement","pricechange","changepercent","volume","amount"};
            @Override
            public StockDay parse(String[] row) {
                try {
                    JSONObject object = new JSONObject();
                    for (int i = 0;i<fields.length;i++){
                        if(!"None".equals(row[i])){
                            if("date".equals(fields[i])){
                                row[i] = row[i].replace("-","");
                            }else if("code".equals(fields[i])){
                                row[i] = row[i].replace("'","");
                            }
                            object.put(fields[i],row[i]);
                        }
                    }
                    return object.toJavaObject(StockDay.class);
                }catch (Exception e){
                    log.error("异常数据：{}",row,e);
                    throw e;
                }
            }
        },1);

        if (list.size()>0) {
            int exec = stockDayMapper.insertMulti(list);
            log.info("插入：{}",exec);         
        }
//        log.info("{}",list);

    }

//
//    code=0000001
//    start=19901219
//    end=20191008
//    fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;VOTURNOVER;VATURNOVER

    @Setter
    @Getter
    public class DownloadParam{
        private String code;
        private String start;
        private String end;
        private String fields;

        public DownloadParam(){
            this.fields = "TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;VOTURNOVER;VATURNOVER";
        }

        public DownloadParam(String code,String start,String end){
            this();
            this.code=code;
            this.start = start;
            this.end = end;
        }
    }

}
